﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MiniErp.Entity.DataStreams;
using MiniErp.Entity.DataStreams.DOs;
using MiniErp.Entity.DataStreams.DOs.AssetManagement;
using MiniErp.Mysql.Log;
using System.Collections.Generic;

namespace MiniErp.Mysql.Data
{
    /// <summary>
    /// mysql数据库上下文
    /// </summary>
    public class MysqlContext : DbContext
    {
        public MysqlContext(DbContextOptions<MysqlContext> options) : base(options)
        {

        }


        /// <summary>
        /// 用户信息表
        /// </summary>
        public DbSet<UserInfo> userInfos { get; set; }
        /// <summary>
        /// 用户权限表
        /// </summary>
        public DbSet<Rights> rights { get; set; }
        /// <summary>
        /// 部门信息表
        /// </summary>
        public DbSet<Department> departments { get; set; }
        /// <summary>
        /// 位置信息表
        /// </summary>
        public DbSet<Position> positions { get; set; }
        /// <summary>
        /// 职称信息表
        /// </summary>
        public DbSet<Titles> titles { get; set; }
        #region AssetManagement
        /// <summary>
        /// 资产状态表
        /// </summary>
        public DbSet<AssetState> assetStates { get; set; }
        /// <summary>
        /// 资产类别表
        /// </summary>
        public DbSet<GeneralType> generalTypes { get; set; }
        /// <summary>
        /// 资产次级类别表
        /// </summary>
        public DbSet<SecondaryType> secondaryTypes { get; set; }
        /// <summary>
        /// 资产信息表
        /// </summary>
        public DbSet<AssetInfo> assetInfos { get; set; }
        /// <summary>
        /// 资产修改记录表
        /// </summary>
        public DbSet<AssetModificationRecord> assetModificationRecords { get; set; }
        /// <summary>
        /// 资产修改类型表
        /// </summary>
        public DbSet<AssetModificationType> assetModificationTypes { get; set; }
        #endregion
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
          
            modelBuilder.Entity<UserInfo>()
                .Navigation(d => d.AssetInfos)
                .AutoInclude(true);
            modelBuilder.Entity<Rights>();
            modelBuilder.Entity<Department>();
            modelBuilder.Entity<Position>();
            modelBuilder.Entity<Titles>();
            #region AssetManagement
            modelBuilder.Entity<AssetState>();
            modelBuilder.Entity<GeneralType>();
            modelBuilder.Entity<SecondaryType>();
            modelBuilder.Entity<AssetInfo>().HasKey(item => new { item.AId, item.GeneralTypeId, item.SecondaryTypeId });
            modelBuilder.Entity<AssetModificationRecord>();
            modelBuilder.Entity<AssetModificationType>();

            #endregion

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //启用懒加载==》Microsoft.EntityFrameworkCore.Proxi
            optionsBuilder.UseLazyLoadingProxies();
#if DEBUG
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddProvider(new EFCoreLoggerProvider());
            //添加EnableSensitiveDataLogging，启用敏感数据记录
            optionsBuilder.UseLoggerFactory(loggerFactory).EnableSensitiveDataLogging();
#endif
        }

    }
}
